<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/3/21
 * Time: 15:17
 */

namespace Api\Domain;

use Bbs\Api\FilterKeyWordsApi;
use Bbs\Api\ForumAttachmentApi;
use Bbs\Api\ForumForumApi;
use Bbs\Api\ForumPostApi;
use Bbs\Api\ForumPostcommentApi;
use Bbs\Api\ForumThreadApi;
use Bbs\Api\ForumUserAdmingroupApi;
use Bbs\Model\ForumAdmingroupModel;
use Bbs\Model\ForumPostModel;
use Ucenter\Api\MemberApi;
use Ucenter\Api\MemberExpApi;

class ForumPostDomain extends BaseDomain{
    protected $business_code = 'forumpost';

    protected $allowType = array("json", "rss", "html");

    /**
     * 帖子查询
     */
    public function query(){

        if(IS_POST){
            $this->checkVersion(100);
            addLog("ForumPost/query",$_GET,$_POST,"应用" . $this->client_id . "调用帖子查询接口");

            $fid = $this->_post('fid','');
            $tid = $this->_post('tid','','主题ID缺失');
            $typeid = $this->_post('typeid',0);
            $curpage = $this->_post('curpage',0);
            $pagesize = $this->_post('pagesize',10);
            $maxcomment = $this->_post('maxcomment',2);
            $view_inc = $this->_post('view_inc',0);

            if($fid === ''){
                $result = apiCall(ForumThreadApi::GET_INFO,array(array('tid'=>$tid)));
                if($result['status']){
                    $fid = $result['info']['fid'];
                }else{
                    $this -> apiReturnErr($result['info']);
                }
            }

            $result = apiCall(ForumPostApi::QUERY_POST,array($fid,$tid,$typeid,$curpage,$pagesize,$maxcomment,$view_inc));
            if($result['status']){
                $this->apiReturnSuc($result['info']);
            }else{
                $this->apiReturnErr($result['info']);
            }


        }else{
            $this->apiReturnErr('只支持POST请求');
        }

    }

    /**
     * 帖子详情
     */
    public function detail(){

        if(IS_POST){
            $this->checkVersion(100);
            addLog("ForumPost/detail",$_GET,$_POST,"应用" . $this->client_id . "调用帖子详情接口");

            $fid = $this->_post('fid','','论坛ID缺失');
            $tid = $this->_post('tid','','主题ID缺失');
            $pid = $this->_post('pid','','帖子ID缺失');

            $result = apiCall(ForumPostApi::DETAIL_POST,array($fid,$tid,$pid));
            if($result['status']){
                $this->apiReturnSuc($result['info']);
            }else{
                $this->apiReturnErr($result['info']);
            }


        }else{
            $this->apiReturnErr('只支持POST请求');
        }
    }



    /**
     * 查询用户信息
     */
    private function getUserInfo($uid,&$arr){
        $result = apiCall('Api/BbsUserCenter/userInfo',array($uid),'Domain');
        if($result['status']){
            $arr['user_info'] = $result['info'];
        }else{
            $this->apiReturnErr('查询用户信息失败');
        }
    }

    /**
     * 查询附件
     */
    private function queryAttachment($tid,$pid,&$arr){

        //查询附件
        $map = array(
            'pid' => $pid,
            'tid' => $tid
        );
        $result = apiCall(ForumAttachmentApi::QUERY_NO_PAGING,array($map,false,array('aid')));
        if($result['status']){
            $arr['img'] = array();
            foreach($result['info'] as $key=>$v){
                if($key >5)break;
                array_push($arr['img'],$v['aid']);
            }

        }

    }

    /**
     * 查询指定条数评论
     */
    private function queryComments($tid,$pid,$maxcomment,&$arr){

        if($arr['comment']==1){
            $arr['view_more'] = 0;
            $map = array(
                'tid' => $tid,
                'pid' => $pid,
            );
            $order['id'] = 'desc';
            $page = array('curpage'=>0,'size'=>$maxcomment);
            $result = apiCall(ForumPostcommentApi::QUERY_WITH_COUNT,array($map,$page,'id desc'));
            if($result['status']){
                if($result['info']['count']<=$maxcomment){
                    //不需要查看更多
                }else{
                    $arr['view_more'] = 1;
                }
                $arr['comment_num'] = $result['info']['count'];
                $arr['comments'] = $result['info']['list'];

                foreach($arr['comments'] as &$val){
                    //时间转换
                    $val['dateline_show'] = date("Y-m-d H:i:s",$val['dateline']);
                    //查询用户信息
                    $this->getUserInfo($val['author_id'],$val);
                }
            }else{
                $this->apiReturnErr('获取'.$arr['pid'].'评论失败');
            }
        }

    }



    /**
     * 评论查询
     */
    public function commentQuery(){
        if(IS_POST){
            $this->checkVersion(100);
            addLog("ForumPost/commentQuery",$_GET,$_POST,"应用" . $this->client_id . "调用评论查询接口");

            $tid = $this->_post('tid','','主题ID缺失');
            $pid = $this->_post('pid','','帖子ID缺失');

            $curpage = $this->_post('curpage',0);
            $pagesize = $this->_post('pagesize',10);

            if($tid == 'rrasj'){
                $tid = 6068;
            }
            if($tid == 'fxphb'){
                $tid = 6067;
            }


            $result = apiCall(ForumPostApi::QUERY_COMMENT,array($tid,$pid,$curpage,$pagesize));
            if($result['status']){
                $this->apiReturnSuc($result['info']);
            }else{
                $this->apiReturnErr($result['info']);
            }


        }else{
            $this->apiReturnErr('只支持POST请求');
        }
    }

    /**
     * 主题回帖
     */
    public function reply(){
        if(IS_POST){
            $this->checkVersion(100);
            addLog("ForumPost/reply",$_GET,$_POST,"应用" . $this->client_id . "调用主题回帖接口");

            $fid = $this->_post('fid','','论坛ID缺失');
            $tid = $this->_post('tid','','主题ID缺失');
            $author = $this->_post('author','','作者缺失');
            $author_id = $this->_post('author_id','','作者ID缺失');
            $subject = $this->_post('subject','');
            $message = $this->_post('message','','回帖内容缺失');
            $attachments = $this->_post('attachments','');

            //关键词过滤
            $subject = $this->filter($subject);
            $message = $this->filter($message);

            $result = apiCall(ForumPostApi::REPLY,array($fid,$tid,$author,$author_id,$subject,$message,$attachments));

            if($result['status']){
                //查询论坛是否需要审核
                $result = apiCall(ForumForumApi::GET_INFO,array(array('fid'=>$fid)));

                if($result['status']){
                    $modnewposts = $result['info']['modnewposts'];
                }else{
                    $this -> apiReturnErr($result['info']);
                }

                //发布帖子+50分 +1经验
                apiCall(MemberApi::RAISE_SCORE,array($author_id,50));
                apiCall(MemberExpApi::EXP_INC,array($author_id,10));

                if($modnewposts==1){
                    //需要审核
                    $this->apiReturnSuc('回帖成功，该论坛需要审核功过后才能显示');
                }else{
                    $this->apiReturnSuc('回帖成功');
                }

            }else{
                $this->apiReturnErr($result['info']);
            }

        }else{
            $this->apiReturnErr('只支持POST请求');
        }
    }

    /**
     * 删除帖子
     */
    public function Delete(){

        if(IS_POST){
            $this->checkVersion(100);
            addLog("ForumPost/userDelete",$_GET,$_POST,"应用" . $this->client_id . "调用删除自己帖子接口");

            $uid = $this->_post('uid','','用户ID缺失');
            $fid = $this->_post('fid','','论坛ID缺失');
            $tid = $this->_post('tid','','主题ID缺失');
            $pid = $this->_post('pid','','帖子ID缺失');

            $result = apiCall(ForumPostApi::DELETE_POST,array($uid,$fid,$tid,$pid));
            if($result['status']){
                $this->apiReturnSuc($result['info']);
            }else{
                $this->apiReturnErr($result['info']);
            }


        }else{
            $this->apiReturnErr('只支持POST请求');
        }

    }


    /**
     * 删除评论
     */
    public function DeleteComment(){

        if(IS_POST){
            $this->checkVersion(100);
            addLog("ForumPost/userDeleteComment",$_GET,$_POST,"应用" . $this->client_id . "调用删除自己评论接口");

            $uid = $this->_post('uid','','用户ID缺失');
            $tid = $this->_post('tid','','主题ID缺失');
            $pid = $this->_post('pid','','帖子ID缺失');
            $id = $this->_post('id','','评论ID缺失');

            $result = apiCall(ForumPostApi::DELETE_COMMENT,array($uid,$tid,$pid,$id));
            if($result['status']){
                $this->apiReturnSuc($result['info']);
            }else{
                $this->apiReturnErr($result['info']);
            }


        }else{
            $this->apiReturnErr('只支持POST请求');
        }
    }

    /**
     * 帖子评论
     */
    public function replyPost(){

        if(IS_POST){
            $this->checkVersion(100);
            addLog("ForumPost/replyPost",$_GET,$_POST,"应用" . $this->client_id . "调用帖子评论接口");

            $pid = $this->_post('pid','','帖子ID缺失');
            $tid = $this->_post('tid','','主题ID缺失');
            $author = $this->_post('author','','作者缺失');
            $author_id = $this->_post('author_id','','作者ID缺失');
            $comment = $this->_post('comment','','评论内容');

            //关键词过滤
            $comment = $this->filter($comment);

            $result = apiCall(ForumPostApi::REPLY_POST,array($pid,$tid,$author,$author_id,$comment));
            if($result['status']){
                //发布评论+10分
                apiCall(MemberApi::RAISE_SCORE,array($author_id,10));
                $this->apiReturnSuc($result['info']);
            }else{
                $this->apiReturnErr($result['info']);
            }


        }else{
            $this->apiReturnErr('只支持POST请求');
        }
    }

    /**
     * 关键词过滤
     */
    private function filter($posts){
        $result = apiCall(FilterKeyWordsApi::FILTER,array($posts));
        return $result['info'];
    }

}

